package com.hsy.baoinbao.modules.order.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.modules.order.gsondata.ReceivingOrderDetail;
import com.hsy.baoinbao.modules.order.gsondata.RecyleListIntoData;
import com.hsy.baoinbao.modules.order.view.EditTextExt;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/27.
 */
public class DetailIntoReceivingActivity extends CommonBaseActivityWithSimpleActionBar {
    private ExpandableListView receive_expand_list;
    private AdapterListView detailGoodsAdapter;
    private TextView code, time, money;
    private String orderId;
    private Intent intent;
    RecyleListIntoData.Result listData;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                listData = DPGsonUtil.fromJson(response, RecyleListIntoData.Result.class);
                code.setText(listData.getCode());
                time.setText(listData.getCreate_datetime());
                money.setText(listData.getCommission());

                detailGoodsAdapter = new AdapterListView(DetailIntoReceivingActivity.this, listData);
                receive_expand_list.setAdapter(detailGoodsAdapter);
                CommonUtils.hideProgressDialog();
            }

        }

        @Override
        public void onError(int what, int type, VolleyError error) {
//            if (0 == what) {
            CommonUtils.hideProgressDialog();
//            }
        }
    };
    private boolean fromNotification = false;

    @Override
    public boolean onActionBarLeftButtonClicked(ImageButton leftButton) {
        if (!fromNotification) {
            return false;
        } else {
            startActivityWithTaskCleared(MainActivity.class);
            return true;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (!fromNotification) {
            return super.dispatchKeyEvent(event);
        } else {
            startActivityWithTaskCleared(MainActivity.class);
            return true;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_into_receiving);
        setTitle("订单详情");
        receive_expand_list = F(R.id.receive_expand_list);
        receive_expand_list.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (getWindow().getCurrentFocus() != null) {
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    getCurrentFocus().clearFocus();
                }
            }
        });

        receive_expand_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (getWindow().getCurrentFocus() != null) {
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    getCurrentFocus().clearFocus();
                }
            }
        });

        receive_expand_list.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (getCurrentFocus() != null) {
                    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    getCurrentFocus().clearFocus();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }

        });
        code = F(R.id.code);
        money = F(R.id.money);
        time = F(R.id.time);


        intent = getIntent();
        orderId = intent.getStringExtra("id");
        fromNotification = intent.getBooleanExtra("fromNotification", false);

        DPLogUtil.d("getIntent" + orderId);
        CommonUtils.showProgressDialog(this);
        Map<String, String> data = new HashMap<>();
        data.put("token", CommonUtils.getToken(DetailIntoReceivingActivity.this));
        data.put("id", orderId);
        HttpUtil.httpPostWithParams(DetailIntoReceivingActivity.this, 0, Consts.API_STROAGE_DETAIL, data, httpCallBack);

    }


    public class AdapterListView extends BaseExpandableListAdapter {
        private List<RecyleListIntoData.Goods> listData;
        private LayoutInflater mInflater;

        public AdapterListView(Context context, RecyleListIntoData.Result src) {
            listData = src.getGoods();
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        //得到子item需要关联的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return (listData.get(childPosition).getChildren());
        }

        //得到子item的ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //设置子item的组件
        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            ChildViewHolder childViewHolder = null;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_detail_receiving_into_child, null);

                childViewHolder = new ChildViewHolder();
                childViewHolder.init(convertView);
            } else {
                childViewHolder = (ChildViewHolder) convertView.getTag();
            }
            childViewHolder.setData(listData.get(groupPosition).getChildren().get(childPosition));
            return convertView;
        }

        //获取当前父item下的子item的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return listData.get(groupPosition).getChildren().size();
        }

        //获取当前父item的数据
        @Override
        public Object getGroup(int groupPosition) {
            return listData.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return listData.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //设置父item组件
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(R.layout.item_detail_receiving_group, null);
            TextView firstName = (TextView) convertView.findViewById(R.id.groupName);
            firstName.setText(listData.get(groupPosition).getName());
            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public class ChildViewHolder {

            public RecyleListIntoData.Children src;

            public TextView nameTextView;
            public TextView priceTextView;
            public TextView unitTextView;
            public TextView showTextView;

            public ChildViewHolder() {

            }

            public void init(View convertView) {
                convertView.setTag(this);

                nameTextView = (TextView) convertView.findViewById(R.id.name);
                priceTextView = (TextView) convertView.findViewById(R.id.price);
                unitTextView = (TextView) convertView.findViewById(R.id.unit);
                showTextView = (TextView) convertView.findViewById(R.id.show);
            }

            public void setData(RecyleListIntoData.Children src) {
                if (null == src) {
                    DPLogUtil.e("src = null.");
                    return;
                }
                this.src = src;
                this.nameTextView.setText(src.getName());
                this.unitTextView.setText(src.getUnit());
                this.showTextView.setText(String.valueOf(src.getQty()));
            }
        }
    }

}
